<?php
/**
 * @author seirios-ls
 * 2021/6/7
 */

namespace YouBaoLian\Http;

class Response
{

    protected $code;

    protected $body;

    protected $header;

    protected $success = false;

    protected $error;

    /**
     * bizContent
     * @var  $bizContent
     */
    private $bizContent;


    protected $resultCode;

    protected $message;

    /**
     * sign
     * @var  $sign
     */
    private $sign;

    /**
     * responseId
     * @var string $responseId
     */
    private $responseId = '';

    /**
     * logsDir
     * @var string $logsDir
     */
    private $logsDir = '';

    public function __construct($code = 500, $body = '', $header = '')
    {
        $this->code = $code;
        $this->header = $header;
        $this->body = $body;

        if ($this->code == 200) {
            $this->success = true;
        }
    }

    public function config(string $responseId = '', string $logsDir = '')
    {
        $this->responseId = $responseId;
        $this->logsDir = $logsDir;
    }

    public function resolve(): Response
    {
        if (!json_decode($this->body)) {
            return $this;
        }

        $this->body = json_decode($this->body, true);
        if (!$this->body) {
            return $this;
        }

        $this->bizContent = $this->body['bizContent'] ?? '';

        $code = $this->body['code'] ?? '500';
        $message = $this->body['message'] ?? '';
        $this->sign = $this->body['sign'] ?? '';

        if ($code === 0 or $code === '0') {
            $this->setCode(200);
            $this->success = true;
        } else {
            $this->code = (string)$code;
            $this->setError($message);
        }

        return $this;
    }

    public function build()
    {
        $this->resultCode = $this->getBizContent('resultCode', 0);


        $this->message = $this->getBizContent('message');

        if ($this->resultCode != 0) {
            $this->setError($this->message);
        }
    }

    /**
     * @return bool
     */
    public function isSuccess(): bool
    {
        return $this->success == true;
    }

    /**
     * @param mixed $error
     */
    public function setError($error): Response
    {
        $this->error = $error;
        $this->success = false;
        return $this;
    }

    /**
     * @return int|mixed
     */
    public function getCode()
    {
        return $this->code;
    }

    /**
     * @param int|mixed $code
     */
    public function setCode(int $code): self
    {
        $this->code = $code;
        return $this;
    }

    /**
     * @return mixed|string
     */
    public function getBody()
    {
        return $this->body;
    }

    /**
     * @param mixed|string $body
     */
    public function setBody($body): self
    {
        $this->body = $body;
        return $this;
    }

    /**
     * @return mixed|string
     */
    public function getHeader()
    {
        return $this->header;
    }

    /**
     * @param mixed|string $header
     */
    public function setHeader($header): self
    {
        $this->header = $header;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getError()
    {
        return $this->error;
    }

    /**
     * @return mixed
     */
    public function getBizContent(string $key = '', $default = '')
    {
        if ($key) {
            return $this->bizContent[$key] ?? $default;
        }
        return $this->bizContent;
    }

    /**
     * @return mixed
     */
    public function getResultCode()
    {
        return $this->resultCode;
    }

    /**
     * @return mixed
     */
    public function getMessage()
    {
        return $this->message;
    }



    /**
     * @return mixed
     */
    public function getSign()
    {
        return $this->sign;
    }

    /**
     * @param mixed $bizContent
     */
    public function setBizContent($bizContent): void
    {
        $this->bizContent = $bizContent;
    }


    public function responseLogs($data)
    {
        if (!$this->logsDir) {
            return;
        }
        if (!$data) {
            $data = '';
        } elseif (is_array($data)) {
            $data = json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
        } elseif (!is_string($data)) {
            return;
        }

        $logs = date('Y-m-d H:i:s') . " DECRYPT  " . $this->responseId . " ";
        $logs .= $data;

        $fileName = $this->logsDir . date('Ym');

        if (!is_dir($fileName)) {
            @mkdir($fileName, 755, true);
        }

        $file = date('Ymd') . '.logs';
        file_put_contents($fileName . '/' . $file, $logs . "\r\n", FILE_APPEND);
    }
}